package com.partsoft.dits.utils;

import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;

import javax.sql.DataSource;

import com.partsoft.dits.log.Log;

public class JdbcTemplate extends org.springframework.jdbc.core.JdbcTemplate {
	
	public JdbcTemplate() {
	}
	
	public JdbcTemplate(DataSource ds) {
		super(ds);
	}
	
	public JdbcTemplate(DataSource ds, boolean lazyInit) {
		super(ds, lazyInit);
	}
	
	@Override
	protected void handleWarnings(Statement stmt) throws SQLException {
		if (isIgnoreWarnings()) {
			if (Log.isDebugEnabled()) {
				SQLWarning warningToLog = stmt.getWarnings();
				while (warningToLog != null) {
					Log.debug("SQLWarning ignored: SQL state '" + warningToLog.getSQLState() + "', error code '" + warningToLog.getErrorCode() + "', message [" + warningToLog.getMessage() + "]");
					try {
						warningToLog = warningToLog.getNextWarning();
					} catch (Throwable e) {
						break;
					}
				}
			}
		}
		else {
			handleWarnings(stmt.getWarnings());
		}
	}

}
